package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.CartMapper;
import org.linlinjava.litemall.db.models.Cart;
import org.linlinjava.litemall.db.service.CartService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart>
        implements CartService {

    @Override
    public Cart queryExist(Long goodsId, Long productId, Long userId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Cart>()
                .eq(Cart::getGoodsId, goodsId)
                .eq(Cart::getProductId, productId)
                .eq(Cart::getUserId, userId));
    }

    @Override
    public List<Cart> queryByUid(Long userId) {
        return baseMapper.selectList(new LambdaQueryWrapper<Cart>()
                .eq(Cart::getUserId, userId));
    }

    @Override
    public List<Cart> queryByUidAndChecked(Long userId) {
        return baseMapper.selectList(new LambdaQueryWrapper<Cart>()
                .eq(Cart::getUserId, userId).eq(Cart::getChecked, true));
    }

    @Override
    public int delete(List<Integer> productIdList, Long userId) {
        QueryWrapper<Cart> query = new QueryWrapper<>();
        query.eq("user_id", userId);
        query.in("product_id", productIdList);
        return baseMapper.delete(query);
    }

    @Override
    public Cart findById(Long userId, Long id) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Cart>()
                .eq(Cart::getUserId, userId).eq(Cart::getId, id));
    }

    @Override
    public int updateCheck(Long userId, List<Integer> idsList, Boolean checked) {
        UpdateWrapper<Cart> update = new UpdateWrapper<>();
        update.eq("user_id", userId);
        update.in("product_id", idsList);
        update.set("checked", true);
        return baseMapper.update(null, update);
    }

    @Override
    public void clearGoods(Long userId) {
        UpdateWrapper<Cart> update = new UpdateWrapper<>();
        update.eq("user_id", userId);
        update.in("checked", true);
        update.set("deleted", true);

       baseMapper.update(null, update);
    }

    @Override
    public IPage<Cart> querySelective(Long userId, Long goodsId, Integer pageNo, Integer pageSize) {
        QueryWrapper<Cart> query = new QueryWrapper<>();
        Page<Cart> page = new Page<>(pageNo, pageSize);
        query.eq(!StringUtils.isEmpty(userId), "user_id", userId);
        query.eq(!StringUtils.isEmpty(goodsId), "goods_id", goodsId);
       return baseMapper.selectPage(page, query);
    }

    @Override
    public boolean checkExist(Long goodsId) {
        QueryWrapper<Cart> query = new QueryWrapper<>();
        query.eq("goods_id", goodsId);
        query.eq("checked", true);
       return baseMapper.selectCount(query) > 0;
    }

    @Override
    public void updateProduct(Long id, String goodsSn, String goodsName, BigDecimal price, String url) {
        Cart cart = new Cart();
        cart.setPrice(price);
        cart.setPicUrl(url);
        cart.setGoodsSn(goodsSn);
        cart.setGoodsName(goodsName);
        UpdateWrapper<Cart> update = new UpdateWrapper<>();
        update.eq("product_id", id);
        baseMapper.update(cart, update);
    }
}
